<?php
include_once ('../../php/postgre.php');
$act = '';
if (isset($_POST['act']))    $act = $_POST['act'];
if ($act == 'simpan_user') {
    $id_akun = $_POST['id_akun'];
    $nama = $_POST['bid-skpd'];
    $level = $_POST['bidang'];
//    $username = $_POST['namaakun'];
//    $pass = $_POST['pass'];
//    $nama = $_POST['nama_pj'];
//    $nip = $_POST['nip_akun'];
    $username = $_POST['kode_skpd'];
	
	if (!is_numeric($id_akun)) {
        echo "Err : invalid id.";
        exit;
    }

    if ($id_akun > 0) {
        exec_query("update mus_akun set id_akun=" . $id_akun . ", id_skpd=" . $id_skpd . ", id_uslevel=" . $level . " where id_akun=".$id_akun);
        echo 'success__';
		logs('Mengubah User level','ID SKPD : '.$id_skpd.'; Level : '.$level);
    } else {
		$rrr = false;
		if ($level < 3)	{
			$maxid = get_maxid('id_akun', 'mus_akun');
			exec_query("insert into mus_akun(id_akun, id_uslevel, namaakun, pass, nama_pj, nip_akun) values(" . $maxid . ", " . $level . ", '" . $username . "', '" . $username . "', '" . $username . "', '" . $username . "')");
			$st = get_data('select id_akun from mus_akun where id_akun=' . $maxid);
			if ($st['id_akun'] == $maxid) {
				echo 'success__';
				$id_akun = $maxid;
			}
		}
		else if ($level == 3){
			$maxid = get_maxid('id_akun', 'mus_akun');
			exec_query("insert into mus_akun(id_akun, id_uslevel, namaakun, pass, nama_pj, nip_akun, id_bidang ) values(" . $maxid . "," . $level . ", '" . $username. "', '" . $username. "', '" . $username. "', '" . $username. "',".$nama.")");
			$st = get_data('select id_akun from mus_akun where id_akun=' . $maxid);
			if ($st['id_akun'] == $maxid) {
				echo 'success__';
				$id_akun = $maxid;
			}
       }
	   else if ($level == 4){
			$maxid = get_maxid('id_akun', 'mus_akun');
			exec_query("insert into mus_akun(id_akun, id_skpd, id_uslevel, namaakun, pass, nama_pj, nip_akun ) values(" . $maxid . ",".$nama.", " . $level . ", '" . $username. "', '" . $username. "', '" . $username. "', '" . $username. "')");
			$st = get_data('select id_akun from mus_akun where id_akun=' . $maxid);
			if ($st['id_akun'] == $maxid) {
				echo 'success__';
				$id_akun = $maxid;
			}
       }
	   else if ($level == 5){
			$maxid = get_maxid('id_akun', 'mus_akun');
			exec_query("insert into mus_akun(id_akun, id_uslevel, namaakun, pass, nama_pj, nip_akun, id_kecamatan) values(" . $maxid . "," . $level . ", '" . $username. "', '" . $username. "', '" . $username. "', '" . $username. "',".$nama.")");
			$st = get_data('select id_akun from mus_akun where id_akun=' . $maxid);
			if ($st['id_akun'] == $maxid) {
				echo 'success__';
				$id_akun = $maxid;
			}
       }
	   else {
			echo "gagal";
			$rrr = true;
			logs('Gagal Menambah User','username : '.$username.'; Level : '.$level.'; Nama : '.$nama);
	   }
	   
	   if(!$rrr){
			logs('Menambah User','username : '.$username.'; Level : '.$level.'; Nama : '.$nama);
	   }
    }

	echo $id_akun . '__';
    view_akun();
} 
else if ($act == 'hapus_user') {
    $id_akun = $_POST['id_akun'];
    if (is_numeric($id_akun)) {
		$uss = get_data('select namaakun,id_uslevel from mus_akun where id_akun='.$id_akun);
        exec_query("delete from mus_akun where id_akun=" . $id_akun);
        echo 'success__';
		logs('Menghapus User','Username : '.$uss['namaakun'].'; Level :'.$uss['id_uslevel']);
    } else {
        echo 'error';
    }
    view_akun();

}
/*else if ($act == 'ubah_akun') {
    $id_akun = $_POST['id_akun'];
    if (is_numeric($id_akun)) {
        $data = get_data("select id_akun, id_uslevel, id_skpd from mus_akun where id_akun=" . $id_akun);
        $ress = implode($data, '__');
        print_r($ress);
        echo '__';
    } else {
        echo 'error';
    }
    view_akun();
} */
 /*else if ($act == 'ambil') {
	$id_level  = $_POST['idlevel'];
    $id_bdskpd = $_POST['idbdskpd'];
	//echo "haaaaaaaaaaaaaaaa".$id_bdskpd;
	$bidang = get_datas("select * from mus_bidang_bappeda");
	$skpd = get_datas("select * from mus_skpd");
			echo $id_level.'.'.$id_bdskpd;
	//	$row = get_data("SELECT kode_skpd from mus_skpd where id_skpd = $id_bdskpd ");
   // $idadmin = ($id_level <= 9)?'0'.$id_level:$id_level;    
  //  echo $idadmin.".".$row['kode_skpd'];
 //   echo $id_level.$id_bdskpd.$row['kode_skpd'];
}*/

/*else if ($act == 'view'){
    view_akun();
}*/

function view_akun() {
    $x = 1;
    $user = get_datas("select id_akun, uslevel,(select nama_skpd from mus_skpd ur where ur.id_skpd=r.id_skpd) as skpd,namaakun, pass, nama_pj, nip_akun, 
(select nama_bdgbapeda from mus_bidang_bappeda b where b.id_bdgbapeda=r.id_bidang) as bidang,
(select nama_kecamatan from mus_kecamatan k where k.id_kecamatan=r.id_kecamatan) as kecamatan
from user_level u, mus_akun r
where u.id_uslevel=r.id_uslevel order by id_akun");
	//if (($user['id_skpd']==NULL) && ($user['id-bidang']==NULL)) {
	//	$data = get_datas("select uslevel, namaakun, pass,namapj, nimakun from mus_akun A, user_level L where A.id_uslevel=L.id_uslevel")
		foreach ($user as $user) {
        ?><tr>
            <td><?php echo $x ?></td>
            <td><?php echo $user['uslevel'] ?></td>
            <td><?php echo $user['skpd']?>
				<?php echo $user['bidang']?>
				<?php echo $user['kecamatan']?></td>
            <td><?php echo $user['namaakun'] ?></td>
            <td><?php echo $user['pass'] ?></td>
            <td><?php echo $user['nama_pj'] ?></td>
            <td><?php echo $user['nip_akun'] ?></td>
            <td>
                <a class="btn btn-danger bt-hapus" name="<?php echo $user['id_akun']; ?>"><i class="icon-trash icon-white"></i></a>
            </td>
        </tr>
        <?php
        $x++;
		}
	//}
	//else if ()
    
}
?>

